package com.thti.mecool.client.command.result.plan;

import java.util.List;

import com.threeti.mecool.mobile.interfaces.dto.ProjectStoreDto;
import com.threeti.mecool.mobile.interfaces.dto.ProjectWithOnlyStoresDto;
import com.thti.mecool.client.activity.MakePlanSelectProjectActivity;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.command.ICommand;
import com.thti.mecool.client.model.biz.ProjectServerBizImpl;
import com.thti.mecool.client.model.po.ProjectServer;
import com.thti.mecool.client.protocol.system.ProjectResultProtocol;
import com.thti.mecool.client.service.Protocol;

public class GetPlanProjectCommand implements ICommand{

	@Override
	public void doCommand(Object o) {
		ProjectResultProtocol projectResultProtocol=(ProjectResultProtocol)o;
		//数据库保存操作
		ProjectServerBizImpl projectServerBizImpl=new ProjectServerBizImpl(Cache.getmContext());
		projectServerBizImpl.delete();
		List<ProjectWithOnlyStoresDto> list=projectResultProtocol.getProjwithstores();
		if(list.size()>0){
			for (int i = 0; i < list.size(); i++) {
				ProjectServer projectServer=new ProjectServer();
				projectServer.setProjectId(list.get(i).getCode());
				projectServer.setName(list.get(i).getName());
				projectServer.setIsType(0);
				projectServerBizImpl.save(projectServer);
				List<ProjectStoreDto> projStores=list.get(i).getProjStores();
				for (int j = 0; j < projStores.size(); j++) {
					ProjectServer project=new ProjectServer();
					project.setStoreId(projStores.get(j).getId());
					project.setName(projStores.get(j).getName());
					project.setIsType(2);
					projectServerBizImpl.save(project);
				}
			}
			//发送成功信息
			BaseActivity.setDataFlag(HandlerConstant.PlanCommand);
			MakePlanSelectProjectActivity.handler.sendEmptyMessage(1);
		}else{
		BaseActivity.setCurrentMesg(projectResultProtocol.getMsg());
		MakePlanSelectProjectActivity.handler.sendEmptyMessage(2);
	}
	}

	@Override
	public String getCommandID() {
		return Protocol.getPlanProject;
	}

	@Override
	public Class<?> getCommandClass() {
		return ProjectResultProtocol.class;
	}

}
